﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace QLThuChi.OtherForms
{
    #region Using
    using QLThuChi.Models;
    using QLThuChi.Repositories;
    using QLThuChi.Helpers;
    using QLThuChi.OtherForms;
    using DevComponents.DotNetBar;
    using QLThuChi.Reports;
    #endregion
    public partial class FrmThongKePhieuChiVLXD : Office2007Form
    {
        // Hidden files are used to store ids 
        private DevComponents.DotNetBar.Controls.TextBoxX txtMaCongTrinh;

        public FrmThongKePhieuChiVLXD()
        {
            InitializeComponent();
            InitHiddenFields();
        }

        /// <summary>
        /// Init hidden fields
        /// </summary>
        public void InitHiddenFields()
        {
            // Add a new textbox
            txtMaCongTrinh = new DevComponents.DotNetBar.Controls.TextBoxX
            {
                Name = "txtMaCongTrinh",
                Text = ""
            };
            txtMaCongTrinh.Visible = false;

        }

        private void FrmThongKePhieuChiVLXD_Load(object sender, EventArgs e)
        {
        }

        private void btnChonCongTrinh_Click(object sender, EventArgs e)
        {
            FrmQuanLyCongTrinh frm = new FrmQuanLyCongTrinh(true);
            frm.Handler += GetCongTrinh;
            frm.ShowDialog();
        }

        public void GetCongTrinh(object sender, EventArgs e)
        {
            var eventType = (MyEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            txtMaCongTrinh.Text = comp[0];
            txtCongTrinh.Text = comp[1];
            LoadGiaDoanBelongToCongTrinh();
        }

        public void LoadGiaDoanBelongToCongTrinh()
        {
            var lstItem = CongTrinh_GiaiDoanRepository.SelectByMaCongTrinh(int.Parse(txtMaCongTrinh.Text));
            cbxGiaiDoan.Items.Clear();

            var all = new CongTrinh_GiaiDoan
            {
                MaGiaiDoan = 0
            };
            all.GiaiDoan = new GiaiDoan
            {
                TenGiaiDoan = "---Tất cả---"
            };
            cbxGiaiDoan.Items.Add(all);

            foreach (var item in lstItem)
            {
                cbxGiaiDoan.Items.Add(item);
            }

            cbxGiaiDoan.SelectedIndex = 0;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnBaoBieu_Click(object sender, EventArgs e)
        {
            if (cbxGiaiDoan.SelectedIndex != -1)
            {
                var giaidoan = (CongTrinh_GiaiDoan)cbxGiaiDoan.SelectedItem;
                var lstItem = new List<PhieuChi_VLXD>();
                // Select all of giai doan
                if (giaidoan.MaCongTrinhGiaiDoan == 0)
                {
                    lstItem = PhieuChi_VLXDRepository.SelectByMaCongTrinh(int.Parse(txtMaCongTrinh.Text));
                }
                else
                {
                    lstItem = PhieuChi_VLXDRepository.SelectByMaCongTrinhGiaiDoan(giaidoan.MaCongTrinhGiaiDoan);
                }

                LapBaoBieu(lstItem);
            }
        }

        public void LapBaoBieu(List<PhieuChi_VLXD> lstItem)
        {
            var dt = new DataTable();
            dt.Columns.Add("STT");
            dt.Columns.Add("GiaiDoan");
            dt.Columns.Add("NguoiLapPhieu");
            dt.Columns.Add("NgayLapPhieu");
            dt.Columns.Add("VLXD");
            dt.Columns.Add("SoLuong");
            dt.Columns.Add("DonGia");

            long tongchiphi = 0;
            for (int i = 0; i < lstItem.Count; i++)
            {
                dt.Rows.Add((i + 1).ToString(), lstItem[i].CongTrinh_GiaiDoan.GiaiDoan.TenGiaiDoan, lstItem[i].NguoiDung.TenNguoiDung, String.Format("{0:dd/MM/yyyy}", lstItem[i].NgayLapPhieu.Value), "", "", "");

                var lstVLXD = ChiTietPhieuRepository.SelectByMaPhieu(lstItem[i].MaPhieu);
                long sum = 0;
                
                for (int j = 0; j < lstVLXD.Count; j++)
                {
                    dt.Rows.Add("", "", "", "", lstVLXD[j].VLXD.TenVLXD, lstVLXD[j].SoLuong, String.Format("{0:#,##0;Nothing}", lstVLXD[j].DonGia));
                    sum += (lstVLXD[j].SoLuong.Value * lstVLXD[j].DonGia.Value);
                }

                tongchiphi += sum;
                dt.Rows.Add("", "", "", "", "THÀNH TIỀN", "", String.Format("{0:#,##0;Nothing}", sum));
            }

            DataView objTimeSheetDV = new System.Data.DataView(dt);
            objTimeSheetDV.AllowNew = false;
            dtgvDataList.AutoGenerateColumns = false;
            dtgvDataList.DataSource = objTimeSheetDV;

            dtgvDataList.ClearSelection();
            txtTongChiPhi.Text = String.Format("{0:#,##0;Nothing}", tongchiphi);
        }

        private void btnInBieu_Click(object sender, EventArgs e)
        {
            if (cbxGiaiDoan.SelectedIndex != -1)
            {
                var giaidoan = (CongTrinh_GiaiDoan)cbxGiaiDoan.SelectedItem;
                var lstItem = new List<PhieuChi_VLXD>();
                // Select all of giai doan
                if (giaidoan.MaCongTrinhGiaiDoan == 0)
                {
                    lstItem = PhieuChi_VLXDRepository.SelectByMaCongTrinh(int.Parse(txtMaCongTrinh.Text));
                }
                else
                {
                    lstItem = PhieuChi_VLXDRepository.SelectByMaCongTrinhGiaiDoan(giaidoan.MaCongTrinhGiaiDoan);
                }

                FrmPrintPhieuChiVLXD frm = new FrmPrintPhieuChiVLXD(lstItem, txtCongTrinh.Text);
                frm.ShowDialog();
            }
        }

        private void dtgvDataList_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (dtgvDataList.Rows[e.RowIndex].Cells[0].Value.ToString().ToUpper() != "")
            {
                e.CellStyle.BackColor = Color.Crimson;
                e.CellStyle.ForeColor = Color.White;
            }
            else if (dtgvDataList.Rows[e.RowIndex].Cells[4].Value.ToString().ToUpper() == "THÀNH TIỀN")
            {
                e.CellStyle.BackColor = Color.OliveDrab;
                e.CellStyle.ForeColor = Color.White;
            }
            else 
            {
                e.CellStyle.BackColor = Color.CornflowerBlue;
                e.CellStyle.ForeColor = Color.White;
            }
        }
    }
}
